package org.itzheng.yuezhi.utils.comm;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取分钟，每分钟调用一次回调
 * Created by admin on 2017/8/13.
 */

public class MinuteBroadcastReceiverUtils {


    private static Map<Object, MinuteBroadcastReceiverUtils> utilsMap = new HashMap<>();

    public static MinuteBroadcastReceiverUtils getInstance(Object tag) {
        MinuteBroadcastReceiverUtils instance = null;
        if (tag != null) {
            instance = utilsMap.get(tag);
            if (instance == null) {
                instance = new MinuteBroadcastReceiverUtils();
                instance.registerReceiver();
                utilsMap.put(tag, instance);
            }
        } else {
            instance = new MinuteBroadcastReceiverUtils();
            instance.registerReceiver();
        }
        return instance;
    }

    //    private OnMinuteChangeListener mListener;
    List<OnMinuteChangeListener> listeners = new ArrayList<>();

    /**
     * 设置广播监听
     *
     * @param onSearchBluetoothListener
     */
    public void addOnSearchBluetoothListener(OnMinuteChangeListener onSearchBluetoothListener) {
        if (onSearchBluetoothListener != null) {
            if (!listeners.contains(onSearchBluetoothListener)) {
                listeners.add(onSearchBluetoothListener);
            }
        }
    }

    public static void removeInstance(Object tag) {
        if (tag != null) {
            if (utilsMap.get(tag) != null) {
                utilsMap.get(tag).unregisterReceiver();
                utilsMap.get(tag).clearOnSearchBluetoothListener();
                utilsMap.remove(tag);
            }
        }
    }

    private void clearOnSearchBluetoothListener() {
        listeners.clear();
    }

    /**
     * 注册广播
     */
    public void registerReceiver() {
//        // 注册接收查找到设备action接收器
//        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
//        UIUtils.getContext().registerReceiver(mReceiver, filter);
//        filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
//        UIUtils.getContext().registerReceiver(mReceiver, filter);
        //时间改变(每分钟监听一次)


        IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK);
        UIUtils.getContext().registerReceiver(mReceiver, filter);
//        /**
//         * 电量改变
//         */
//        filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
//        UIUtils.getContext().registerReceiver(mReceiver, filter);
//        filter = new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION);
//        UIUtils.getContext().registerReceiver(mReceiver, filter);
    }

    /**
     * 反注册广播监听
     */
    public void unregisterReceiver() {
        try {
            UIUtils.getContext().unregisterReceiver(mReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 蓝牙搜索监听
     */
    public interface OnMinuteChangeListener {

        void onMinuteChange();
    }


    /**
     * 查找到设备和搜索完成action监听器
     */
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        private static final String TAG = "BroadcastReceiver";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.e(TAG, "onReceive:" + action);
            switch (action) {
                //屏幕打开
                case Intent.ACTION_TIME_TICK:
                    if (listeners != null) {
                        for (OnMinuteChangeListener listener : listeners) {
                            listener.onMinuteChange();
                        }
                    }
                    break;
            }
        }
    };
}
